package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Brand;
import pojo.Car;
import pojo.CarType;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Nicholas
 * @version 1.0
 */
public class ManageCarDB {
    private List<Car> carsList;
    private Log log;


    public ManageCarDB(Log log) {
        carsList =  DataBase.getInstance().getCarsList();
        this.log = log;
    }
    public ManageCarDB(List<Car> list){
        carsList = list;
        log = LogFactory.getLog(getClass());
    }

    //添加车辆
    public boolean add(Car car) {
        if(car!=null){
            if (find(car) == -1) {
                carsList.add(car);
                log.info(getClass()+"\tadd\t"+ SystemEnum.SUCCESS.toString());
                return true;
            }else {
                log.info(getClass()+"\tadd\t"+ SystemEnum.ERROR_REPEAT.toString());
                return false;
            }
        }
        log.info(getClass()+"\tadd\t"+ SystemEnum.ERROR_MATCH.toString());
        return false;
    }

    //查找车辆
    public int find(Car find){
        int index = 0;
        if(find!=null){
            for(Car f : carsList){
                if(f.equals(find)) break;
                index+=1;
            }
            if(index<carsList.size()){
                log.info(getClass()+"\tfind\t"+SystemEnum.SUCCESS.toString());
                return index;
            }
        }
        log.info(getClass()+"\tfind\t"+SystemEnum.ERROR_MATCH.toString());
        return -1;
    }
    //查找车辆--按字段
    public List<Car> find(String field,String content){
        List<Car> findByField = new ArrayList<>();
        if(content==null) return findByField;
        try {
            switch (field){
                case "carNum":
                    for(Car find : carsList){
                        if(find.getCarNum().equals(content)) findByField.add(find);
                    }
                    break;
                case "disPlacement":
                    for(Car find : carsList){
                        if(find.getDisPlacement()==Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "releaseTime>":
                    for(Car find : carsList){
                        if(compare(find.getReleaseTime(),LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")))<0) findByField.add(find);
                    }
                    break;
                case "releaseTime=":
                    for(Car find : carsList){
                        if(compare(find.getReleaseTime(),LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")))==0) findByField.add(find);
                    }
                    break;
                case "releaseTime<":
                    for(Car find : carsList){
                        if(compare(find.getReleaseTime(),LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")))>0) findByField.add(find);
                    }
                    break;
                case "price>":
                    for(Car find : carsList){
                        if(find.getPrice() > Double.valueOf(content)) findByField.add(find);
                    }
                case "price<=":
                    for(Car find : carsList){
                        if(find.getPrice()<=Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "mileage>":
                    for(Car find : carsList){
                        if(find.getMileage()>Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "mileage=":
                    for(Car find : carsList){
                        if(find.getMileage()==Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "mileage<":
                    for(Car find : carsList){
                        if(find.getMileage()<Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "clutchType":
                    for(Car find : carsList){
                        if(find.getClutchType().equals(content)) findByField.add(find);
                    }
                    break;
                case "licensingTime>":
                    for(Car find : carsList){
                        if(compare(find.getLicensingTime(),LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")))<0) findByField.add(find);
                    }
                    break;
                case "licensingTime<=":
                    for(Car find : carsList){
                        if(compare(find.getLicensingTime(),LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")))>=0) findByField.add(find);
                    }
                    break;
                case "brand":
                    for(Car find : carsList){
                        if(find.getBrand().equals(Brand.valueOf(content))) findByField.add(find);
                    }
                    break;
                case "carType":
                    for(Car find : carsList){
                        if(find.getBrand().equals(CarType.valueOf(content))) findByField.add(find);
                    }
                    break;
                default:
                    log.info(SystemEnum.ERROR_INPUT.toString());
            }
        }catch (Exception e){
            log.info(SystemEnum.ERROR_INPUT.toString());
            return null;
        }
        return findByField;
    }

    //删除车辆
    public boolean delete(Car car){
        if(car!=null){
            int index = find(car);
            if(index == -1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                carsList.remove(index);
                log.info(getClass()+"\tdelete\t"+SystemEnum.SUCCESS.toString());
                return true;
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    //按照最新发布日期的车辆进行排序
    public List<Car> showLatest() {
        Car car = null;
        for (int i = 0; i < carsList.size() - 1; i++) {
            for (int j = 0; j < carsList.size() - i - 1; j++) {
                if (compare(carsList.get(j), carsList.get(j + 1)) < 0) {
                    car = carsList.get(j);
                    carsList.set(j, carsList.get(j + 1));
                    carsList.set(j + 1, car);
                }
            }
        }
        return carsList;
    }

    public int compare(Car car1, Car car2) {
        LocalDate releaseTime1 = car1.getReleaseTime();
        LocalDate releaseTime2 = car2.getReleaseTime();
        if(releaseTime1.equals(releaseTime2))return 0;
        else if(releaseTime1.isBefore(releaseTime2))return -1;
        else return 1;
    }
    public int compare(LocalDate time1,LocalDate time2){
        if(time1.equals(time2))return 0;
        else if(time1.isBefore(time2))return -1;
        else return 1;
    }

    //改
    public boolean modify(Car user,String field,String content){
        if(user!=null){
            int index = find(user);
            if(index==-1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                Car find = carsList.get(index);
                try {
                    switch (field) {
                        case "carNum":
                            find.setCarNum(content);
                            break;
                        case "disPlacement":
                            find.setDisPlacement(Double.valueOf(content));
                            break;
                        case "releaseTime":
                            find.setReleaseTime(LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            break;
                        case "price":
                            find.setPrice(Double.valueOf(content));
                            break;
                        case "mileage":
                            find.setMileage(Double.valueOf(content));
                            break;
                        case "clutchType":
                            find.setClutchType(content);
                            break;
                        case "licensingTime":
                            find.setLicensingTime(LocalDate.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            break;
                        case "brand":
                            find.setBrand(Brand.valueOf(content));
                            break;
                        case "carType":
                            find.setCarType(CarType.valueOf(content));
                            break;
                        default:
                            log.info(SystemEnum.ERROR_INPUT.toString());
                    }
                    log.info(getClass()+"\tmodify\t"+SystemEnum.SUCCESS.toString());
                    return true;
                }catch (Exception e){
                    log.info(SystemEnum.ERROR_INPUT.toString());
                    return false;
                }
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    public void printAll(){
        System.out.println(carsList);
    }
    public void printCar(List<Car> cars){
        System.out.println("  品牌\t车型\t\t里程数\t\t价格\t\t\t发布时间");
        for (Car car:cars) {
            System.out.println((cars.indexOf(car)+1)+"."+car.getBrand().getBrandName()+"\t"+car.getCarType().getName()+"\t"+car.getMileage()+"\t\t"+car.getPrice()+"\t\t"+car.getReleaseTime());
        }

    }
    public List<Car> getCarsList(){
        return carsList;
    }
    public int getSize(){
        return carsList.size();
    }
}
